package it.low.game.core.element;

import it.low.game.core.location.MapPoint;

import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.RectangularShape;

public abstract class AreaElement extends Element {

	private RectangularShape mapAreaShape = null;
		
	public void init(int size, MapPoint mapPosition){
		this.mapAreaWidth = size;
		this.mapAreaHeight = size/3;
		this.mapAreaShape = new Ellipse2D.Double(0,0,mapAreaWidth,mapAreaHeight);
		this.setMapPosition(mapPosition);
	}
	
	public RectangularShape getMapArea(){
		return mapAreaShape;
	}
	
	@Override
	public void setMapPosition(MapPoint mapPosition) {
		super.setMapPosition(mapPosition);
		double x = mapPosition.x-mapAreaWidth/2;
		this.mapAreaShape.setFrame(x, mapPosition.y-mapAreaHeight, mapAreaWidth, mapAreaHeight);
	}
		
	public boolean isClose(Element element){
		if(super.isClose(element)){
			Area a1 = new Area(this.mapAreaShape);
			if(element instanceof AreaElement){
				// THIS IS VALID ONLY FOR AREA ELEMENTS
				Area a2 = new Area(((AreaElement)element).mapAreaShape);
				a1.intersect(a2);
				return a1!=null && !a1.isEmpty();
			}else{
				return a1.contains(element.getMapPosition());
			}
		}else{
			return false;
		}
	}
	
}
